<?php

namespace App\Admin\Controllers\SendOrder;

use App\Http\Controllers\Controller;
use App\Models\SendOrderPattern;
use App\Models\Service;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Illuminate\Http\Request;

class SendOrderPatternController extends AdminController
{
    protected $title = '抢单模式管理';


    /**
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SendOrderPattern());

        $grid->model()->with('service');

        $grid->column('service.name', '服务类别');

        $grid->column('level', '轮次')->display(function ($level) {
            return SendOrderPattern::LEVEL_NOTE[$level];
        });

        $grid->column('distance', '距离')->display(function ($distance){
            return $distance.SendOrderPattern::DISTANCE_UNIT;
        });

        $grid->disableCreateButton();
        $grid->disableDeleteButton();
        $grid->disableViewButton();

        return $grid;
    }


    /**
     * 表单
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SendOrderPattern());

        $form->id;

        $form->select('level', '轮次')->options(SendOrderPattern::LEVEL_NOTE)->disable()->saving(function () use($form){
            return $form->model()->level;
        });

        $form->select('service_id', '业务类别')->options(Service::selectOption())->disable()->saving(function () use ($form){
            return $form->model()->service_id;
        });

        $form->number('distance', '距离'.SendOrderPattern::DISTANCE_UNIT)->min(1)->required();

        $form->disableDeleteButton();

        return $form;
    }
}
